package com.example.springretry.config;

import com.example.springretry.service.PayService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.retry.RetryException;
import org.springframework.retry.annotation.EnableRetry;
import org.springframework.retry.backoff.FixedBackOffPolicy;
import org.springframework.retry.policy.SimpleRetryPolicy;
import org.springframework.retry.support.RetryTemplate;

import java.util.HashMap;
import java.util.Map;

/**
 * @author jimao.zou
 * @date 9/27/2018 2:14 PM
 * @description: TODO
 */
@Configuration
@EnableRetry
public class RetryConfiguration {
    @Bean
    public RetryTemplate retryTemplate() {
        RetryTemplate retryTemplate = new RetryTemplate();

        FixedBackOffPolicy fixedBackOffPolicy = new FixedBackOffPolicy();
        fixedBackOffPolicy.setBackOffPeriod(2000L);
        retryTemplate.setBackOffPolicy(fixedBackOffPolicy);

        Map<Class<? extends Throwable>, Boolean> exceptionMaps = new HashMap<>();
        exceptionMaps.put(RetryException.class, true);

        SimpleRetryPolicy retryPolicy =
                new SimpleRetryPolicy(Integer.valueOf(4), exceptionMaps);

        retryTemplate.setRetryPolicy(retryPolicy);

        return retryTemplate;
    }

}
